package conchmusic.conchmusicclient.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import conchmusic.conchmusicclient.pojo.Lyric;
import conchmusic.conchmusicclient.util.LrcUtils;

/**
 * Created by sbml1 on 2017/4/22.
 */

public class LrcViewDemo extends TextView{
    private List<Lyric> lyrics;
    private int currentLine = 0;
    private final int lineSpace = getTextSizeByScrean(22);
    private Lyric lyric = null;
    private String source="";
    private URL url;

    private Paint currentPaint;
    private int currentColor = Color.GREEN;
    private final int currentTextSize = getTextSizeByScrean(22);
    private Typeface currentTypeface = Typeface.DEFAULT_BOLD;

    private Paint otherPaint;
    private int otherColor = Color.BLACK;
    private final int otherTextSize = getTextSizeByScrean(18);
    private Typeface otherTypeface = Typeface.SERIF;
    private Context context;
    private String sourceTemp="";
    private LrcUtils lrcUtils=new LrcUtils();

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            invalidate();
            super.handleMessage(msg);
        }
    };


    //设置歌词文件路径
    public void setSource(String Source){
        this.source=Source;
        if(source!=null && !source.equals(sourceTemp)){
            lyrics=lrcUtils.getLrcByPath(source);
            sourceTemp=source;
        }

    }


    public LrcViewDemo(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;

        currentPaint = new Paint();
        otherPaint = new Paint();


        currentPaint.setColor(currentColor);
        currentPaint.setTextSize(currentTextSize);
        currentPaint.setTextAlign(Paint.Align.CENTER); // 画在中间
        currentPaint.setTypeface(currentTypeface);

        otherPaint.setColor(otherColor);
        otherPaint.setTextSize(otherTextSize);
        otherPaint.setTextAlign(Paint.Align.CENTER);
        otherPaint.setTypeface(otherTypeface);
    }

    @Override
    protected void onDraw(final Canvas canvas) {

        if (lyrics != null && currentLine < lyrics.size()) {

            //绘制播放过的歌词
            for (int i = currentLine - 1; i >= 0; i--) {
                lyric = lyrics.get(i);
                if(lyric.lricString==null){
                    lyric.lricString="";
                }
                canvas.drawText(lyric.lricString, getWidth() / 2,
                        getHeight() / 2 + lineSpace * (i - currentLine), otherPaint);
            }
            lyric = lyrics.get(currentLine);
            if(lyric.lricString==null){
                lyric.lricString="";
            }
            // 绘制正在播放的歌词
            canvas.drawText(lyric.lricString, getWidth() / 2,
                    getHeight() / 2, currentPaint);
            //绘制未播放的歌词
            for (int i = currentLine + 1; i < lyrics.size(); i++) {
                lyric = lyrics.get(i);
                if(lyric.lricString==null){
                    lyric.lricString="";
                }
                canvas.drawText(lyric.lricString, getWidth() / 2,
                        getHeight() / 2 + lineSpace * (i - currentLine), otherPaint);
            }
        } else {
            canvas.drawText("未找到歌词", getWidth() / 2,
                    getHeight() / 2, currentPaint);
        }
        super.onDraw(canvas);
    }

    //追踪歌词进度
    public void changeCurrent(long Time){
        if(lyrics==null || lyrics.size()==0){
            handler.sendEmptyMessage(0);
            return;
        }

        if(Time<lyrics.get(1).timePint){
            currentLine=0;
            handler.sendEmptyMessage(0);
            return;
        }

        if(Time>lyrics.get(lyrics.size()-1).timePint && lyrics.size()>=1){
            currentLine=lyrics.size()-1;
            handler.sendEmptyMessage(0);
            return;
        }
//        if(lyrics.get(currentLine).timePint<Time){
//            currentLine=currentLine+1;
//        }
        if(currentLine<=0){
            currentLine=1;
        }

//        if(currentLine==0){
//            currentLine=1;
//        }

        if(lyrics.get(currentLine-1).timePint<Time && Time<lyrics.get(currentLine).timePint){
            return;
        }
        for(int i=0;i<lyrics.size();i++){
            if(lyrics.get(i).timePint>Time){
                currentLine=i-1;
                if(currentLine<=0){
                    currentLine=0;
                }
                handler.sendEmptyMessage(0);
                return;
            }
        }
    }


    public void freshLrcView(){
        handler.sendEmptyMessage(0);
    }

    //根据屏幕大小获得歌词字号
    private int getTextSizeByScrean(int originTextSize){
        int textSize=0;
        DisplayMetrics displayMetrics=new DisplayMetrics();
        WindowManager wm=(WindowManager)this.getContext().getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(displayMetrics);
        int width=displayMetrics.widthPixels;
        int height=displayMetrics.heightPixels;
        float ratioWidth=(float)width/480;
        float ratioHeight=(float)height/800;
        final float RATIO=Math.min(ratioWidth,ratioHeight);
        textSize=Math.round(originTextSize*RATIO);
        return textSize;
    }

}